package com.shopper.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shopper.manager.config.ConfigCode;
import com.shopper.manager.config.RequestCode;
import com.shopper.manager.config.RequestMassage;
import com.shopper.manager.mapper.IndexMapper;
import com.shopper.manager.mapper.OrderMapper;
import com.shopper.manager.model.customer.req.OrderListReq;
import com.shopper.manager.model.index.ShopAddressBean;
import com.shopper.manager.model.index.UserBean;
import com.shopper.manager.model.index.UserResultBean;
import com.shopper.manager.model.index.req.InsertClockTimeReq;
import com.shopper.manager.model.order.*;
import com.shopper.manager.model.order.req.*;
import com.shopper.manager.model.order.result.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController extends BaseController {

    @Autowired
    private OrderMapper orderMapper;//订单相关数据

    @Autowired
    private IndexMapper personMapper;//用户相关数据


    /**
     * 这里是新增后，拿的id
     * @param header
     * @param bean
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/index/insertCategory")
    public String insertCategory(@RequestHeader Map<String, String> header, @RequestBody OrderCategoryBean bean) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        if (bean != null) {
            bean.setBusinessLicense(userBean.getBusinessLicense());
            int code = orderMapper.insertCategory(bean);
            OrderCategoryBean categoryBean=null;
            if (code==1){
                categoryBean=orderMapper.selectCategoryById(bean.getId());
            }

            return returnResultInfo(categoryBean != null ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED,categoryBean);
        }
        return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE, null);


    }


    @PostMapping("s1/index/selectCategory")
    @JsonProperty(value = "DOI")
    public String selectCategory(@RequestHeader Map<String, String> header) throws JsonProcessingException {
        String businessLicense="";
        if (header!=null&&!isEmpty(header.get("businesslicense"))){
            businessLicense=header.get("businesslicense");
        }else {
            String token = processToken(header);
            if (!token.equals(ConfigCode.TOKEN_OK)) {
                return token;
            }
            UserBean userBean = baseMapper.selectUserById(getUid(header));
            businessLicense=userBean.getBusinessLicense();
        }

        List<OrderCategoryBean> list = orderMapper.selectCategory(businessLicense);
        return returnResultInfo(RequestCode.SUCCESS_CODE, list);
    }


    @PostMapping("s1/index/deleteCategory")
    public String deleteCategory(@RequestHeader Map<String, String> header, @RequestBody OrderCategoryBean bean) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (bean == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        bean.setBusinessLicense(userBean.getBusinessLicense());
        int code = orderMapper.deleteCategory(bean);
        return returnResultInfo(code == 1 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_DELETE_FAILED);
    }

    @PostMapping("s1/index/insertAttribute")
    public String insertAttribute(@RequestHeader Map<String, String> header, @RequestBody AttributeBean bean) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        if (bean != null && bean.getType() > 0 && bean.getName() != null) {
            UserBean userBean = baseMapper.selectUserById(getUid(header));
            bean.setBusinessLicense(userBean.getBusinessLicense());
            int code = 0;
            switch (bean.getType()) {
                case 1://规格
                    code = orderMapper.insertOrderSpecifications(bean);
                    break;
                case 2://温度
                    code = orderMapper.insertOrderTemperature(bean);
                    break;
                case 3://糖度
                    code = orderMapper.insertOrderSugar(bean);
                    break;
                case 4://配料
                    code = orderMapper.insertOrderIngredients(bean);
                    break;

            }
            return returnResultInfo(code == 1 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED);
        }

        return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);


    }

    @PostMapping("s1/index/selectListAttribute")
    public String selectListAttribute(@RequestHeader Map<String, String> header) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        List<OrderBean> list = new ArrayList<>();
        list.add(geOrderBeanResult(1, "规格", userBean.getBusinessLicense()));
        list.add(geOrderBeanResult(2, "温度", userBean.getBusinessLicense()));
        list.add(geOrderBeanResult(3, "糖度", userBean.getBusinessLicense()));
        list.add(geOrderBeanResult(4, "配料", userBean.getBusinessLicense()));
        return returnResultInfo(RequestCode.SUCCESS_CODE, list);
    }

    /**
     * 获取属性请求结果
     *
     * @param type
     * @param attributeName
     * @return
     */
    private OrderBean geOrderBeanResult(int type, String attributeName, String businessLicense) {
        OrderBean bean = new OrderBean();
        bean.setType(type);
        bean.setAttributeName(attributeName);
        bean.setListAttribute(orderMapper.getListAttribute(getAttributeCode(type, businessLicense,0)));
        return bean;
    }

    private AttributeBean getAttributeCode(int type, String businessLicense,int goodsId) {
        AttributeBean bean = new AttributeBean();
        bean.setBusinessLicense(businessLicense);
        bean.setType(type);
        bean.setGoodsId(goodsId);
        return bean;

    }


    @PostMapping("s1/index/insertGoods")
    public String insertGoods(@RequestHeader Map<String, String> header, @RequestBody GoodsBean bean) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (bean == null) return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        OrderCategoryBean orderCategoryBean=new OrderCategoryBean();
        orderCategoryBean.setId(bean.getGoodsCategory());
        orderCategoryBean.setBusinessLicense(userBean.getBusinessLicense());
        int count=orderMapper.countCategory(orderCategoryBean);
        if (count<=0){
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }
        int specificationCode=countAttribute(bean.getSpecificationsId(), 1);
        if (specificationCode <= 0) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }

        int temperatureCode=countAttribute(bean.getTemperatureId(), 2);
        if (temperatureCode <= 0) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }

        int sugarIdCode=countAttribute(bean.getSugarId(), 3);
        if (sugarIdCode <= 0) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }
        int ingredientsCode=countAttribute(bean.getIngredientsId(), 4);
        if ( ingredientsCode<= 0) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }
        bean.setBusinessLicense(userBean.getBusinessLicense());
        int code = orderMapper.insertGoods(bean);
        insertAttribute(bean.getSpecificationsId(), 1,userBean.getBusinessLicense(),bean.getId());
        insertAttribute(bean.getTemperatureId(), 2,userBean.getBusinessLicense(),bean.getId());
        insertAttribute(bean.getSugarId(), 3,userBean.getBusinessLicense(),bean.getId());
        insertAttribute(bean.getIngredientsId(), 4,userBean.getBusinessLicense(),bean.getId());


        return returnResultInfo(code == 1 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED);
    }

    /**
     * 判断属性是否存在
     * @param data
     * @param type
     * @return
     */
    private int countAttribute(String data, int type) {

        if (data != null && data.length() > 0) {
            String[] split = data.split(",");
            for (String id : split) {
                AttributeBean attributeBean = new AttributeBean();
                attributeBean.setId(Integer.valueOf(id));
                attributeBean.setType(type);
                AttributeCodeBean attributeCodeBean = orderMapper.countAttribute(attributeBean);
                if (attributeCodeBean==null){
                    return 0;
                }

            }

            return 1;
        }

        return 1001;
    }

    private void insertAttribute(String data,int type,String businessLicense,int goodsId){
        if (data != null && data.length() > 0) {
            String[] split = data.split(",");
            for (String id : split) {
                AttributeBean attributeBean = new AttributeBean();
                attributeBean.setId(Integer.valueOf(id));
                attributeBean.setType(type);
                attributeBean.setGoodsId(goodsId);
                attributeBean.setBusinessLicense(businessLicense);
                AttributeListBean listBean=orderMapper.selectAttributeListBean(attributeBean);
                if (listBean==null){
                    AttributeCodeBean attributeCodeBean = orderMapper.countAttribute(attributeBean);
                    AttributeListBean attributeListBean=new AttributeListBean();
                    attributeListBean.setAttributeId(Integer.valueOf(id));
                    attributeListBean.setAttributeType(type);
                    attributeListBean.setBusinessLicense(businessLicense);
                    attributeListBean.setGoodsId(goodsId);
                    attributeListBean.setName(attributeCodeBean.getName());
                    orderMapper.insertAttributeList(attributeListBean);
                }

            }
        }
    }



    @PostMapping("s1/index/getCategoryGoodsList")
    public String getCategoryGoodsList(@RequestHeader Map<String, String> header, @RequestBody OrderCategoryBean bean) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        bean.setBusinessLicense(userBean.getBusinessLicense());
        List<GoodsBean> list=orderMapper.getGoodsList(bean);
        List<GoodsListBean> listGoods=new ArrayList<>();
        for (GoodsBean b:list){
            GoodsListBean glb=new GoodsListBean();
            glb.setGoodsCategory(bean.getId());
            glb.setGoodsDesc(b.getGoodsDesc());
            glb.setGoodsImage(b.getGoodsImage());
            glb.setGoodsPrice(b.getGoodsPrice());
            glb.setGoodsName(b.getGoodsName());
            glb.setId(b.getId());
            glb.setShelvesType(b.getShelvesType());
            glb.setGoodsRanking(b.getGoodsRanking());

            List<OrderBean> listAttribute=new ArrayList<>();
            listAttribute.add(getOrderBean(1,"规格",userBean.getBusinessLicense(),b.getId()));
            listAttribute.add(getOrderBean(2,"温度",userBean.getBusinessLicense(),b.getId()));
            listAttribute.add(getOrderBean(3,"糖度",userBean.getBusinessLicense(),b.getId()));
            listAttribute.add(getOrderBean(4,"配料",userBean.getBusinessLicense(),b.getId()));
             glb.setList(listAttribute);
            listGoods.add(glb);
        }


        return returnResultInfo(RequestCode.SUCCESS_CODE, listGoods);

    }

    private OrderBean getOrderBean(int type,String typeName,String business,int goodsId){
        OrderBean  orderBean=new OrderBean();
        orderBean.setType(type);
        orderBean.setAttributeName(typeName);
        orderBean.setListAttribute(orderMapper.getGoodsAttribute(getAttributeCode(type, business,goodsId)));

        return orderBean;
    }


    @PostMapping("s1/order/deleteGoods")
    public String deleteGoods(@RequestHeader Map<String, String> header, @RequestBody DeleteGoodsReq bean) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (bean == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        bean.setBusinessLicense(userBean.getBusinessLicense());
        int code = orderMapper.deleteGoods(bean);
//        orderMapper.deleteGoodsCategory(bean);
        return returnResultInfo(code == 1 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_DELETE_FAILED);
    }



    @PostMapping("s1/index/updateCategoryPosition")
    public String updateCategoryPosition(@RequestHeader Map<String, String> header, @RequestBody List<OrderCategoryBean> list) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (list == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        for (OrderCategoryBean b:list){
            b.setBusinessLicense(userBean.getBusinessLicense());
        }

        int code = orderMapper.updateCategoryPosition(list);

        return returnResultInfo(code >0 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED);
    }

    /**
     * 编辑品类信息
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/editCategory")
    public String editCategory(@RequestHeader Map<String, String> header, @RequestBody EditCategoryReq req) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (req == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }

        int code = orderMapper.editCategory(req);


        return returnResultInfo(code >0 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED,code >0?req:null);
    }


    /**
     * 新增品类规格
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/insertNorms")
    public String insertNorms(@RequestHeader Map<String, String> header, @RequestBody NormsInsertReq req) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (req == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        req.setBusinessLicense(userBean.getBusinessLicense());
        req.setNormsTime(System.currentTimeMillis());
        int code = orderMapper.insertNorms(req);

        return returnResultInfo(code >0 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED,code >0?req:null);
    }

    /**
     * 新增品类规格属性
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/insertNormsAttribute")
    public String insertNormsAttribute(@RequestHeader Map<String, String> header, @RequestBody NormsAttributeInsertReq req) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (req == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        req.setBusinessLicense(userBean.getBusinessLicense());
        req.setAttributeTime(System.currentTimeMillis());
        int code = orderMapper.insertNormsAttribute(req);

        return returnResultInfo(code >0 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED,code >0?req:null);
    }


    /**
     * 获取规格列表
     * @param header
     * @param bean
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/getNormsList")
    public String getNormsList(@RequestHeader Map<String, String> header, @RequestBody NormsListReq bean) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        bean.setBusinessLicense(userBean.getBusinessLicense());
        List<NormsBean> list=orderMapper.getNormsList(bean);
        if (list!=null&&list.size()>0){
            for (NormsBean normsBean:list){
                List<NormsAttributeInsertReq> listAttribute=orderMapper.getNormsAttributeList(normsBean.getId());
                normsBean.setListAttribute(listAttribute);
            }
        }

        return returnResultInfo(RequestCode.SUCCESS_CODE, list);

    }


    /**
     * 新增订单商品
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/insertOrderGoods")
    public String insertOrderGoods(@RequestHeader Map<String, String> header, @RequestBody InsertGoodsReq req) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (req == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        req.setBusinessLicense(userBean.getBusinessLicense());
        int code = orderMapper.insertOrderGoods(req);
        if (code>0){
            GoodsListReq goodsReq=new GoodsListReq();
            goodsReq.setCategoryId(req.getCategoryId());
            goodsReq.setId(req.getId());

            List<GoodsResultBean> bean= orderMapper.getGoodInfo(goodsReq);
            return returnResultInfo(RequestCode.SUCCESS_CODE,bean);

        }

        return returnResultInfo( RequestCode.ERROR_UPDATE_FAILED,null);
    }


    /**
     * 新增订单商品
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/editOrderGoods")
    public String editOrderGoods(@RequestHeader Map<String, String> header, @RequestBody InsertGoodsReq req) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (req == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        req.setBusinessLicense(userBean.getBusinessLicense());
        int code = orderMapper.editOrderGoods(req);
        if (code>0){
            GoodsListReq goodsReq=new GoodsListReq();
            goodsReq.setCategoryId(req.getCategoryId());
            goodsReq.setId(req.getId());
            List<GoodsResultBean> bean= orderMapper.getGoodInfo(goodsReq);
            return returnResultInfo(bean !=null ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED,bean);

        }

        return returnResultInfo(code >0 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED,code >0?req:null);
    }



    /**
     * 查询订单商品
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/getCategoryGoodList")
    public String getCategoryGoodList(@RequestHeader Map<String, String> header, @RequestBody GoodsListReq req) throws JsonProcessingException {
        if (req == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }

       if (isEmpty(req.getBusinessLicense())){
           String token = processToken(header);
           if (!token.equals(ConfigCode.TOKEN_OK)) {
               return token;
           }

           if (req == null) {
               return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
           }
           UserBean userBean = baseMapper.selectUserById(getUid(header));
           req.setBusinessLicense(userBean.getBusinessLicense());
       }
        List<GoodsResultBean> list = orderMapper.getCategoryGoodList(req);
        for (GoodsResultBean bean:list){

             if (bean.getNormsList()!=null&&!bean.getNormsList().isEmpty()){
                 List<NormsBean> listNorms=new ArrayList<>();
                 List<NormsIds> listNormsId= JSONObject.parseObject(bean.getNormsList(), new TypeReference<List<NormsIds> >(){});
                 if (listNormsId!=null&& listNormsId.size()>0){
                     for(int i=0;i<listNormsId.size();i++){
                         NormsIds ids=listNormsId.get(i);
                         NormsReq normsReq=new NormsReq();
                         normsReq.setId(ids.getNormsId());
                         NormsBean normsBean=orderMapper.getOrderNorms(normsReq);
                         GoodsNormsAttributeReq  attributeReq=new GoodsNormsAttributeReq();
                         attributeReq.setNormsId(ids.getNormsId());
                         attributeReq.setIds(ids.getList());
                         List<NormsAttributeInsertReq> listAttribute=orderMapper.getGoodNormsAttributeList(attributeReq);
                         if (listAttribute!=null&& listAttribute.size()>0){
                             normsBean.setListAttribute(listAttribute);
                         }
                         if (normsBean!=null){
                             listNorms.add(normsBean);
                         }

                     }
                 }
                 bean.setListNorms(listNorms);
             }
        }

        return returnResultInfo(RequestCode.SUCCESS_CODE, list);
    }


    /**
     * 删除规格属性
     */
    @PostMapping("s1/order/deleteNormsAttribute")
    public String deleteNormsAttribute(@RequestHeader Map<String, String> header, @RequestBody DeleteNormsAttributeReq bean) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (bean == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }

        int code = orderMapper.deleteAttribute(bean.getId());
        return returnResultInfo(code == 1 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_DELETE_FAILED);
    }


    /**
     * 删除规格
     */
    @PostMapping("s1/order/deleteNorms")
    public String deleteNorms(@RequestHeader Map<String, String> header, @RequestBody DeleteNormsAttributeReq bean) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (bean == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }

        int code = orderMapper.deleteNorms(bean.getId());
        return returnResultInfo(code >= 1 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_DELETE_FAILED);
    }


    /**
     * 获取店铺详情
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/getShopperDetail")
    public String getShopperDetail(@RequestHeader Map<String, String> header) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        UserBean userBean = baseMapper.selectUserById(getUid(header));
        ShopperDetailResult  detailResult=new ShopperDetailResult();
        ShopAddressBean sab = personMapper.selectShopAddress(userBean.getShopAddressId());
        if (sab != null) {
            UserResultBean.ShopAddress sAddress = new UserResultBean.ShopAddress();
            sAddress.setId(sab.getId());
            sAddress.setLatitude(sab.getLatitude());
            sAddress.setLongitude(sab.getLongitude());
            sAddress.setShopAddress(sab.getShopAddress());
            detailResult.setAddress(sAddress);

        }
        InsertClockTimeReq clockTimeReq=personMapper.selectBusinessClockTime(userBean.getBusinessLicense());
        if (clockTimeReq!=null){
            UserResultBean.ClockTime clockTime=new UserResultBean.ClockTime();
            clockTime.setEndTime(clockTimeReq.getEndTime());
            clockTime.setStartTime(clockTimeReq.getStartTime());
            detailResult.setClickTime(clockTime);
        }
        return returnResultInfo( RequestCode.SUCCESS_CODE,detailResult);

    }



    /**
     * 分页查询订单列表
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/getOrderList")
    public String getOrderList(@RequestHeader Map<String, String> header, @RequestBody OrderListReq  req) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        req.setBusinessLicense(userBean.getBusinessLicense());


        return returnResultInfo( RequestCode.SUCCESS_CODE,findPage(req));

    }




    public Map<String,Object> findPage(OrderListReq req){
        Map<String,Object>resultMap=new HashMap<>();
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<OrderListResultBean> list = orderMapper.selectOrderList(req);
        PageInfo<Map<String,OrderListResultBean>> pageInfo = new PageInfo(list);
        resultMap.put("count",pageInfo.getTotal());//获得总数
        resultMap.put("pageNum",req.getPageNum());//当前页数
        resultMap.put("pageSize",req.getPageSize());//当前数量
        for (OrderListResultBean resultBean:list){
            List<OrderGoodsBean> goodsList =new ArrayList<>();
            List<OrderIdResultBean> listOrderGoods= JSONObject.parseObject(resultBean.getGoodGroup(), new TypeReference<List<OrderIdResultBean>>(){});
                for (OrderIdResultBean orderIdResultBean :listOrderGoods){
                    OrderGoodsReq orderGoodsReq=new OrderGoodsReq();
                    orderGoodsReq.setCategoryId(orderIdResultBean.getCategoryId());
                    orderGoodsReq.setGoodId(orderIdResultBean.getGoodId());
                    OrderGoodsBean orderGoodsBean=orderMapper.selectOrderGoods(orderGoodsReq);
                    if (orderGoodsBean!=null){
                        orderGoodsBean.setNum(orderIdResultBean.getNum());
                        List<NormsAttributeInsertReq> listAttribute=new ArrayList<>();
                        for (OrderIdResultBean.NormsIdBean idBean :orderIdResultBean.getNormsList()){
                            OrderGoodsNormsReq goodsNormsReq=new OrderGoodsNormsReq();
                            goodsNormsReq.setAttributeId(idBean.getAttributeId());
                            goodsNormsReq.setNormsId(idBean.getNormsId());
                            NormsAttributeInsertReq attributeInsertReq=orderMapper.selectOrderGoodsNorms(goodsNormsReq);
                            if (attributeInsertReq==null){
                                continue;
                            }
                            listAttribute.add(attributeInsertReq);
                        }
                        orderGoodsBean.setNormsAttributeList(listAttribute);
                        goodsList.add(orderGoodsBean);
                    }

                }
            resultBean.setGoodsList(goodsList);
        }

        resultMap.put("data",list);
        return resultMap;
    }



    /**
     * 分页查询用户订单列表
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/getUserOrderList")
    public String getUserOrderList(@RequestHeader Map<String, String> header, @RequestBody OrderListReq req) throws JsonProcessingException {

        String token = processCustomerToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }


        return returnResultInfo( RequestCode.SUCCESS_CODE,findUserOrderPage(req));

    }


    public Map<String,Object> findUserOrderPage(OrderListReq req){
        Map<String,Object>resultMap=new HashMap<>();
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<OrderListResultBean> list = orderMapper.selectUserOrderList(req);
        PageInfo<Map<String,OrderListResultBean>> pageInfo = new PageInfo(list);
        resultMap.put("count",pageInfo.getTotal());//获得总数
        resultMap.put("pageNum",req.getPageNum());//当前页数
        resultMap.put("pageSize",req.getPageSize());//当前数量
        for (OrderListResultBean resultBean:list){
            List<OrderGoodsBean> goodsList =new ArrayList<>();
            List<OrderIdResultBean> listOrderGoods= JSONObject.parseObject(resultBean.getGoodGroup(), new TypeReference<List<OrderIdResultBean>>(){});
            for (OrderIdResultBean orderIdResultBean :listOrderGoods){
                OrderGoodsReq orderGoodsReq=new OrderGoodsReq();
                orderGoodsReq.setCategoryId(orderIdResultBean.getCategoryId());
                orderGoodsReq.setGoodId(orderIdResultBean.getGoodId());
                OrderGoodsBean orderGoodsBean=orderMapper.selectOrderGoods(orderGoodsReq);
                if (orderGoodsBean!=null){
                    orderGoodsBean.setNum(orderIdResultBean.getNum());
                    List<NormsAttributeInsertReq> listAttribute=new ArrayList<>();
                    for (OrderIdResultBean.NormsIdBean idBean :orderIdResultBean.getNormsList()){
                        OrderGoodsNormsReq goodsNormsReq=new OrderGoodsNormsReq();
                        goodsNormsReq.setAttributeId(idBean.getAttributeId());
                        goodsNormsReq.setNormsId(idBean.getNormsId());
                        NormsAttributeInsertReq attributeInsertReq=orderMapper.selectOrderGoodsNorms(goodsNormsReq);
                        if (attributeInsertReq==null){
                            continue;
                        }
                        listAttribute.add(attributeInsertReq);
                    }
                    orderGoodsBean.setNormsAttributeList(listAttribute);
                    goodsList.add(orderGoodsBean);
                }

            }
            resultBean.setGoodsList(goodsList);
        }

        resultMap.put("list",list);
        return resultMap;
    }





    /**
     * 更新订单状态
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/updateOrderState")
    public String updateOrderState(@RequestHeader Map<String, String> header, @RequestBody OrderStateReq  req) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        int state=orderMapper.updateOrderState(req);

        return returnResultInfo(state==1? RequestCode.SUCCESS_CODE:RequestCode.ERROR_UPDATE_FAILED);

    }

    /**
     * 获得商品排名
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/order/getGoodsSort")
    public String getGoodsSort(@RequestHeader Map<String, String> header) throws JsonProcessingException {

        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        List<com.shopper.manager.model.customer.req.OrderGoodsReq> list=orderMapper.getGoodsSort(userBean.getBusinessLicense());

        return returnResultInfo( RequestCode.SUCCESS_CODE,list);

    }


}
